package com.top.ruizhong.mode.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.module.lemlin.base.BaseViewModel
import com.module.lemlin.mode.ViewModelSubscribe
import com.module.lemlin.rxhttp.HttpServiceData
import com.module.lemlin.rxhttp.HttpServiceResponse
import com.top.ruizhong.entity.*
import com.top.ruizhong.main.video.VideoKeepActivity
import com.top.ruizhong.mode.api.*
import com.top.ruizhong.mode.manager.UploadProgress
import com.top.ruizhong.mode.manager.commonUpload
import com.top.ruizhong.mode.manager.indexAudioAdd
import com.top.ruizhong.mode.manager.indexAudioList
import io.reactivex.rxjava3.disposables.Disposable
import rxhttp.wrapper.entity.Progress
import java.io.File

class VideoViewModel(application: Application) : BaseViewModel(application) {


    val uploadLiveData = MutableLiveData<HttpServiceResponse<UploadResponse>>()
    val progressLiveData = MutableLiveData<Progress>()

    val audioAddLiveData = MutableLiveData<HttpServiceResponse<HttpServiceData>>()
    val audioListLiveData = MutableLiveData<HttpServiceResponse<AudioListResponse>>()

    /**
     * 上传视频
     * */
    fun commonUpload(path: String, type: UploadResponse.Type = UploadResponse.Type.FILE_NULL) {
        if (path.isNullOrEmpty())
            return
        val progress = object : UploadProgress {
            override fun onProgress(progress: Progress) {
                progressLiveData.postValue(progress)
            }
        }
        val listener = object : ViewModelSubscribe<UploadResponse>(uploadLiveData) {
            override fun onSuccess(resp: UploadResponse) {
                super.onSuccess(resp.apply { this.type = type })
            }
        }
        val disposable: Disposable =
            commonUpload(File(path), progress, listener)
        dispose.add(disposable)
    }

    /**
     * 添加视频
     * */
    fun indexAudioAdd(
        title: String, url: String, content: String = "内容！"
    ) {
        val listener = ViewModelSubscribe<HttpServiceData>(audioAddLiveData)
        val disposable: Disposable =
            indexAudioAdd(title, url, content, listener)
        dispose.add(disposable)
    }

    private var page: Int = 1
    private var limit: Int = 10

    /**
     * 视频列表
     * */
    fun indexAudioList(isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<AudioListResponse>(audioListLiveData) {
            override fun onSuccess(resp: AudioListResponse) {
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable =
            indexAudioList(page, limit, listener)
        dispose.add(disposable)
    }


    val videoListLiveData = MutableLiveData<HttpServiceResponse<VideoListResponse>>()

    /**
     * 视频列表数据
     *
     * @param limit 条数
     * @param cateId 分类ID: -2朋友 -1推荐
     * */
    fun videoIndex(cateId: Int = -2, isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<VideoListResponse>(videoListLiveData) {
            override fun onSuccess(resp: VideoListResponse) {
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable =
            videoIndex(limit.toString(), cateId.toString(), listener)
        dispose.add(disposable)
    }

    val videoCateDefaultListLiveData = MutableLiveData<HttpServiceResponse<VideoCateListResponse>>()

    /**
     * 默认分类列表（顶上的分类列表）
     * */
    fun videoDefaultCateList() {
        val disposable: Disposable =
            videoDefaultCateList(ViewModelSubscribe(videoCateDefaultListLiveData))
        dispose.add(disposable)
    }

    val videoCateTotalListLiveData = MutableLiveData<HttpServiceResponse<VideoCateListResponse>>()

    /**
     * 所有分类列表
     * */
    fun videoCateTotalList() {
        val disposable: Disposable =
            videoCateList(ViewModelSubscribe(videoCateTotalListLiveData))
        dispose.add(disposable)
    }

    val videoSetCateLiveData = MutableLiveData<HttpServiceResponse<VideoCateSetResponse>>()

    /**
     * 设置分类
     *
     * @param cateIds 选中的分类（逗号隔开，推荐和朋友不加入）
     * */
    fun videoSetCate(cateIds: String, videoCateList: VideoCateList, type: Int) {
        val listener = object : ViewModelSubscribe<VideoCateSetResponse>(videoSetCateLiveData) {
            override fun onSuccess(resp: VideoCateSetResponse) {
                super.onSuccess(resp.apply {
                    this.videoCateList = videoCateList
                    this.type = type
                })
            }
        }
        val disposable: Disposable = videoSetCate(cateIds, listener)
        dispose.add(disposable)
    }


    val videoAddOrEditLiveData = MutableLiveData<HttpServiceResponse<HttpServiceData>>()

    /**
     * 添加视频
     * */
    fun videoAdd(request: VideoRequest) {
        val disposable: Disposable =
            videoAdd(request.apply {
                action = null
                path_advert = null
                path_bitmap = null
                path_video = null
            }, ViewModelSubscribe(videoAddOrEditLiveData))
        dispose.add(disposable)
    }

    fun videoEdit(request: VideoRequest) {
        val disposable: Disposable =
            videoEdit(request.apply {
                action = null
                path_advert = null
                path_bitmap = null
                path_video = null
            }, ViewModelSubscribe(videoAddOrEditLiveData))
        dispose.add(disposable)
    }

    val videoDetailsLiveData = MutableLiveData<HttpServiceResponse<VideoDetailsResponse>>()

    /**
     * 视频详情
     *
     * @param videoId 视频id,多个用逗号隔开
     * */
    fun videoDetail(videoId: Int) {
        val disposable: Disposable =
            videoDetail(videoId.toString(), ViewModelSubscribe(videoDetailsLiveData))
        dispose.add(disposable)
    }
}